﻿using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web.Services;
using TripLOA.WebService.Serializable;

namespace TripLOA.WebService.Rest
{
    [WebService(Namespace = "http://triploa.cli.di.unipi.it/service/rest/", Name = "RestUser")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class RestUser : EndPoint
    {

        #region RestUser Constructors

        public RestUser()
        {}

        #endregion
        
        #region RestUser methods

        public string login(NameValueCollection _REQUEST, string format)
        {
            //modificati parametri user in username e passwd in password
            WSUser wsu = UserCore.Instance.Login(_REQUEST["username"], _REQUEST["password"]);
            return Serialization.serialize(wsu, typeof(WSUser), format);
        }

        public string getProfile(NameValueCollection _REQUEST, string format)
        {
            
            //Validate parameters
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["user_id"] });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }
            WSUserProfile up = UserCore.Instance.GetProfile(_REQUEST["auth_token"], int.Parse(_REQUEST["user_id"]));
            return Serialization.serialize(up, typeof(WSUserProfile), format);
        }

        public string GetUserVehicleList(NameValueCollection _REQUEST, string format)
        {
            WSUserVehicle uv = UserCore.Instance.GetUserVehicleList(_REQUEST["auth_token"]);
            return Serialization.serialize(uv, typeof(WSUserVehicle), format);
        }

        public string GetUserNotificationsList(NameValueCollection _REQUEST, string format)
        {
            WSNotification not = UserCore.Instance.GetUserNotifications(_REQUEST["auth_token"]);
            return Serialization.serialize(not, typeof(WSNotification), format);
        }

        public string GetUserFriendshipRequests(NameValueCollection _REQUEST, string format)
        {
            WSUser not = UserCore.Instance.GetUserFriendshipRequests(_REQUEST["auth_token"]);
            return Serialization.serialize(not, typeof(WSUser), format);
        }

        public string GetUserFriendshipNotifications(NameValueCollection _REQUEST, string format)
        {
            WSUser not = UserCore.Instance.GetUserFriendshipNotifications(_REQUEST["auth_token"]);
            return Serialization.serialize(not, typeof(WSUser), format);
        }

        public string AcceptFriendshipRequest(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["friend_id"] });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }
            WSBool wsb = UserCore.Instance.AcceptFriendshipRequest(_REQUEST["auth_token"], int.Parse(_REQUEST["friend_id"]));
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        public string DenyFriendshipRequest(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["friend_id"] });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }
            WSBool wsb = UserCore.Instance.DenyFriendshipRequest(_REQUEST["auth_token"], int.Parse(_REQUEST["friend_id"]));
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        public string GetBuddyList(NameValueCollection _REQUEST, string format)
        {
            WSUser buddyList = UserCore.Instance.GetBuddyList(_REQUEST["auth_token"]);
            return Serialization.serialize(buddyList, typeof(WSUser), format);
        }

        public string GetBannedList(NameValueCollection _REQUEST, string format)
        {
            WSUser bannedList = UserCore.Instance.GetBannedList(_REQUEST["auth_token"]);
            return Serialization.serialize(bannedList, typeof(WSUser), format);
        }

        public string AddFriend(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["friend_id"] });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }
            WSBool wsb = UserCore.Instance.AddFriend(_REQUEST["auth_token"], int.Parse(_REQUEST["friend_id"]));
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        public string RemoveFriend(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["friend_id"] });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }
            WSBool wsb = UserCore.Instance.RemoveFriend(_REQUEST["auth_token"], int.Parse(_REQUEST["friend_id"]));
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        public string BanFriend(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["friend_id"] });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }
            WSBool wsb = UserCore.Instance.BanFriend(_REQUEST["auth_token"], int.Parse(_REQUEST["friend_id"]));
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        public string UnbanFriend(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["friend_id"] });
            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }
            WSBool wsb = UserCore.Instance.UnbanFriend(_REQUEST["auth_token"], int.Parse(_REQUEST["friend_id"]));
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        #endregion
    }
}
